package com.prime.tutorial.moviestore.dao;


import com.prime.tutorial.moviestore.domain.City;
import com.prime.tutorial.moviestore.domain.Country;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import java.util.List;

@Repository
public class CityDAOWithJPA extends GenericDAOWithJPA<City, Long> implements CityDAO {

    //	@SuppressWarnings("unchecked")
    public List<City> findByName(String name) {
        Query query = entityManager.createQuery("Select distinct m from City m where m.name = ?1");
        query.setParameter(1, name);
        return query.getResultList();
//		return getJpaTemplate().find("Select m from Groups m where m.name = ?1",name);
    }

    public List<City> findWithDevicesInCountry(Country country) {
        Query query = entityManager.createQuery("Select distinct ci from City ci, Country co, Location l, Device d where d.location = l.id and l.country = "+country.getId()+" and l.city = ci.id");
//        query.setParameter(1, country.getId());
        return query.getResultList();

    }
}